/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package smartgrid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author claudio
 */
public class GraphJSON {

    public static String ListToJSon(Collection<DataLine> list, String name, List<Annotation> ans) {
        HashMap<Date, Annotation> anm = new HashMap<Date, Annotation>();
        for(Annotation an : ans)
            anm.put(an.getDate(), an);
        StringBuilder sb = new StringBuilder();
        sb.append(name);
        sb.append("=[");
        Iterator<DataLine> it = list.iterator();
        while(it.hasNext()){
            DataLine dl = it.next();
            if(anm.containsKey(dl.getTimeStamp())){
                sb.append("{y:");
                sb.append(dl.toString());
                sb.append(",marker:{fillColor : '#C4C4C4', lineColor: '#C4C4C4', lineWidth: 3, radius : 3}}");
                
            }
            else
                sb.append(dl.toString());
            if(it.hasNext())
                sb.append(",");
        }
        sb.append("]");
        return sb.toString();
    }
    
    public static String AnnotationsToJSon(List<Annotation> ans){
        StringBuilder sb = new StringBuilder();
        sb.append("annotations={");
        Iterator<Annotation> it = ans.iterator();
        while(it.hasNext()){
            Annotation an = it.next();
            sb.append("'");
            sb.append(an.getDate().getDate());
            sb.append("-");
            sb.append(an.getDate().getMonth());
            sb.append("-");
            sb.append((an.getDate().getYear() + 1900));
            sb.append("':'");
            sb.append(an.getName());
            sb.append("'");
            if(it.hasNext())
                sb.append(",");
        }
        sb.append("}");
        return sb.toString();
    }
    
    public static String StringToJSon(String value, String name){
        StringBuilder sb = new StringBuilder();
        sb.append(name);
        sb.append("='");
        sb.append(value);
        sb.append("'");
        return sb.toString();
    }
    
    
    String title;
    String subtitle;
    String yAxixTitle;
    ArrayList<String> xAxix;
    ArrayList<String> yAxix;
    ArrayList<GraphLine> series;

    /* Caracteristicas da linha */
    String value = "0", width = "1", color = "#808080";
    
    /* Construtores */
    /**
     * Construtor de graficos
     * @param title titulo do grafico
     * @param yAxixTitle titulo do eixo dos Y
     * @param subtitle subtitulo do grafico
     * @param xAxix valores do eixo dos Y
     * @param yAxix valores do eixo dos X
     * @param series lista de linhas
     */
    public GraphJSON(String title, String yAxixTitle, String subtitle, ArrayList<String> xAxix, ArrayList<String> yAxix, ArrayList<GraphLine> series) {
        this.title = title;
        this.yAxixTitle = yAxixTitle;
        this.subtitle = subtitle;
        this.xAxix = xAxix;
        this.yAxix = yAxix;
        this.series = series;
    }

    /**
     * Construtor para uma linha apenas!
     * @param title titulo do grafico
     * @param yAxixTitle titulo do eixo dos Y
     * @param subtitle subtitulo do grafico
     * @param xAxix valores do eixo dos Y
     * @param yAxix valores do eixo dos X
     * @param valoresLinha array com a lista de valores para a linha
     * @param nomeLinha nome da linha
     */
    public GraphJSON(String title, String yAxixTitle, String subtitle, ArrayList<String> xAxix, ArrayList<String> yAxix, ArrayList<String> valoresLinha, String nomeLinha) {
        this.title = title;
        this.yAxixTitle = yAxixTitle;
        this.subtitle = subtitle;
        this.xAxix = xAxix;
        this.yAxix = yAxix;
        GraphLine gl = new GraphLine(nomeLinha, valoresLinha);
        this.series = new ArrayList<GraphLine>();
        this.series.add(gl);
    }

    /**
     * Construtor para uma linha apenas e sem subtitulo!
     * @param title titulo do grafico
     * @param yAxixTitle titulo do eixo dos Y
     * @param xAxix valores do eixo dos Y
     * @param yAxix valores do eixo dos X
     * @param valoresLinha array com a lista de valores para a linha
     * @param nomeLinha nome da linha
     */
    public GraphJSON(String title, String yAxixTitle, ArrayList<String> xAxix, ArrayList<String> yAxix, ArrayList<String> valoresLinha, String nomeLinha) {
        this.title = title;
        this.yAxixTitle = yAxixTitle;
        this.subtitle = "";
        this.xAxix = xAxix;
        this.yAxix = yAxix;
        GraphLine gl = new GraphLine(nomeLinha, valoresLinha);
        this.series = new ArrayList<GraphLine>();
        this.series.add(gl);
    }

    /**
     * Construtor de graficos sem titulo
     * @param title titulo do grafico
     * @param yAxixTitle titulo do eixo dos Y
     * @param xAxix valores do eixo dos Y
     * @param yAxix valores do eixo dos X
     * @param series lista de linhas
     */
    public GraphJSON(String title, String yAxixTitle, ArrayList<String> xAxix, ArrayList<String> yAxix, ArrayList<GraphLine> series) {
        this.title = title;
        this.yAxixTitle = yAxixTitle;
        this.subtitle = "";
        this.xAxix = xAxix;
        this.yAxix = yAxix;
        this.series = series;
    }

    public GraphJSON() {
        title = "";
        subtitle = "";
    }

    /* Setters */
    public void setSeries(ArrayList<GraphLine> series) {
        this.series = series;
    }

    public void setSubtitle(String subtitle) {
        this.subtitle = subtitle;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setxAxix(ArrayList<String> xAxix) {
        this.xAxix = xAxix;
    }

    public void setyAxix(ArrayList<String> yAxix) {
        this.yAxix = yAxix;
    }

    public void setyAxixTitle(String yAxixTitle) {
        this.yAxixTitle = yAxixTitle;
    }

    public void setyAxixPorperties(String value, String width, String color){
        this.value = value;
        this.width = width;
        this.color = color;
    }
    
    /* Getters */
    public ArrayList<GraphLine> getSeries() {
        return series;
    }

    public String getyAxixTitle() {
        return yAxixTitle;
    }

    public String getSubtitle() {
        return subtitle;
    }

    public String getTitle() {
        return title;
    }

    public ArrayList<String> getxAxix() {
        return xAxix;
    }

    public ArrayList<String> getyAxix() {
        return yAxix;
    }

    /* JSON */
    public String getyAxixJSON() {
        JSONObject jsonobject = new JSONObject();

        try {
            /* Caracteristicas da linha: */
            JSONArray array = new JSONArray();
            JSONObject arrayValue = new JSONObject();
            arrayValue.put("value", value);
            arrayValue.put("width", width);
            arrayValue.put("color", color);
            array.put(arrayValue);


            jsonobject.put("title", yAxixTitle);
            jsonobject.put("plotLines", array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonobject.toString();
    }

    public String getxAxixJSON() {
        JSONObject jsonobject = new JSONObject();

        try {
            jsonobject.put("categories", xAxix);
        } catch (JSONException e) {
        }
        return jsonobject.toString();
    }

    public String getSeriesJSON() {
        JSONArray array = new JSONArray();
        JSONObject arrayValue = new JSONObject();

        try {
            for (GraphLine line : series) {
                arrayValue.put("name", line.getName());
                arrayValue.put("data", line.getData());
                array.put(arrayValue);
            }
        } catch (JSONException e) {
        }
        return array.toString();
    }

    public static void main(String[] args) {

        ArrayList<String> array = new ArrayList<String>();
        array.add("Jan");
        array.add("Feb");
        array.add("Mar");
        array.add("Apr");
        /*GraphLine grLine = new GraphLine("cenas", array);
        GraphLine grLine2 = new GraphLine("SEGUNDA", array);
        ArrayList<GraphLine> arraygrLine = new ArrayList<GraphLine>();
        arraygrLine.add(grLine);
        arraygrLine.add(grLine2);

        GraphJSON graph = new GraphJSON("titulo", "temperatura", "subtitulo", array, array, arraygrLine);


        System.out.println(graph.getSeriesJSON());
        System.out.println(graph.getxAxixJSON());
        System.out.println(graph.getyAxixJSON());
        
        graph = new GraphJSON("titulo", "axisTitle", array, array, array,"linha Coijo");

        System.out.println("\n\n");
        /*System.out.println(graph.getSeriesJSON());
        System.out.println(graph.getxAxixJSON());
        System.out.println(graph.getyAxixJSON());*/
        
        //System.out.println(GraphJSON.ListToJSon(array));
    }
}
